package Dao;

import Bean.Join;
import Bean.OrderGoods;
import Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JoinDao {

    /**
     *
     * @param i i=1时 查询所有数据; i=2时 根据订单号和入库时间查询
     * @param where where[0]为订单号  where[1]为入库时间
     * @return
     */
    public static List<Join> getJoinInfo(int i,String[] where){
        List<Join> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "";
        try {
            con = JDBCUtils.getConnection();
            if(i==1){
              sql  = "select * from tb_joindepot";
              pst = con.prepareStatement(sql);
            }else if(i==2){
                if("".equals(where[1])){
                    sql = "select * from tb_joindepot where oid = ?";
                    pst = con.prepareStatement(sql);
                    pst.setString(1,where[0]);
                }else {
                    sql  = "select * from tb_joindepot where oid = ? and joinTime like ?";
                    pst = con.prepareStatement(sql);
                    pst.setString(1,where[0]);
                    pst.setString(2,"%"+where[1]+"%");
                }

            }
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                Join j = new Join();
                j.setId(rs.getInt(1));
                j.setOid(rs.getInt(2));
                j.setDid(rs.getInt(3));
                j.setName(rs.getString(4));
                j.setJoinTime(rs.getString(5));
                j.setWeight(rs.getFloat(6));
                j.setRemark(rs.getString(7));
                list.add(j);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static  Boolean addJoinInfo(Join j){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "insert into tb_joindepot value(?,?,?,?,?,?,?)";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,j.getId());
            pst.setInt(2,j.getOid());
            pst.setInt(3,j.getDid());
            pst.setString(4,j.getName());
            pst.setString(5,j.getJoinTime());
            pst.setFloat(6,j.getWeight());
            pst.setString(7,j.getRemark());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }

    public static List<Integer> findId(){
        List<Integer> idList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "select id from tb_joindepot";
            //执行sql
            pst = con.prepareStatement(sql);

            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                idList.add(rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return idList;
    }

    public static boolean updateJoinInfo(Join j){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "update  tb_joindepot set oid = ?,did = ?,wareName = ?,joinTime = ?,weight = ?,remark = ? where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,j.getOid());
            pst.setInt(2,j.getDid());
            pst.setString(3,j.getName());
            pst.setString(4,j.getJoinTime());
            pst.setFloat(5,j.getWeight());
            pst.setString(6,j.getRemark());
            pst.setInt(7,j.getId());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }

    public static boolean deleteJoinInfo(Join j){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from tb_joindepot where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,j.getId());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }
}
